package com.ruoyi.web.controller.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.WxLoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.WxTokenService;
import com.ruoyi.task.domain.TaskConfig;
import com.ruoyi.task.domain.TaskExamine;
import com.ruoyi.task.domain.TaskManagement;
import com.ruoyi.task.domain.VO.SubmitTaskVO;
import com.ruoyi.task.domain.VO.ViewsAddedVO;
import com.ruoyi.task.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@Api(tags = "首页相关接口")
@RestController
@RequestMapping("/api/home")
@Slf4j
public class HomeController extends BaseController {


    @Autowired
    private ITaskCarouselService iTaskCarouselService;

    @Autowired
    private WxTokenService wxTokenService;

    @Autowired
    private ITaskManagementService iTaskManagementService;

    @Autowired
    private ITaskExamineService iTaskExamineService;

    @Autowired
    private ITaskImageService iTaskImageService;

    @Autowired
    private ITaskUserService iTaskUserService;

    @ApiOperation("获取banner图片")
    @GetMapping("getBanner")
    public AjaxResult getBanner(HttpServletRequest request){
        return success(iTaskCarouselService.selectTaskCarouselList(null));
    }

    @ApiOperation("获取最新任务")
    @GetMapping("getNewTask")
    public TableDataInfo getNewTask(HttpServletRequest request,TaskManagement taskManagement){
        log.info("获取最新任务11 {}",taskManagement);
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        log.info("获取最新任务12 {}",wxLoginUser);
        if (null != wxLoginUser){
            log.info("获取最新任务13 {}",taskManagement.getUserId());
            taskManagement.setUserId(wxLoginUser.getUserId());
        }else{
            taskManagement.setUserId(0L);
        }
        log.info("获取最新任务13 {}",taskManagement.getUserId());
        startPage();
        List<TaskManagement> list = iTaskManagementService.getNewTask(taskManagement.getUserId());
        list.stream().forEach(taskManagement1 -> {
            if (taskManagement1.getEndTime() != null && taskManagement1.getEndTime().before(new Date())){
                if (taskManagement.getUserId() != 0L){
                    taskManagement1.setStatus("1");
                    iTaskManagementService.updateTaskManagementById(taskManagement1);
                }
            }
        });
        return getDataTable(list);
    }

    @ApiOperation("获取任务详情 ?taskId=1")
    @GetMapping("getNewTaskByTaskId")
    public AjaxResult getNewTask(HttpServletRequest request, Long taskId){
        return success(iTaskManagementService.selectTaskManagementById(taskId));
    }

    @ApiOperation("提交任务")
    @PostMapping("submitTask")
    public AjaxResult submitTask(HttpServletRequest request ,@Validated @RequestBody SubmitTaskVO submitTaskVO ){
        log.info("提交任务 {}",submitTaskVO);
        TaskExamine taskExamine = new TaskExamine();
        taskExamine.setTaskId(submitTaskVO.getTaskId());
        taskExamine.setUserId(wxTokenService.getWxUser(request).getUserId());
        int i = iTaskExamineService.insertTaskExamine(taskExamine);
        iTaskImageService.deleteTaskImageById(taskExamine.getId());
        TaskManagement taskManagement = new TaskManagement();
        taskManagement.setId(submitTaskVO.getTaskId());
        taskManagement.setParticipants(1L);
        if (i == 1){
            iTaskManagementService.updateTaskManagementById(taskManagement);
        }
        submitTaskVO.getTaskImageList().stream().forEach(taskImage -> {
            taskImage.setExamineId(taskExamine.getId());
            iTaskImageService.insertTaskImage(taskImage);
        });
        return success(i);
    }


    @ApiOperation("新增浏览量")
    @PostMapping("newViewsAdded")
    public AjaxResult newViewsAdded(HttpServletRequest request ,@Validated @RequestBody ViewsAddedVO submitTaskVO ){
        log.info("新增浏览量 {}",submitTaskVO);
        //WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        TaskManagement taskManagement = new TaskManagement();
        taskManagement.setId(submitTaskVO.getTaskId());
        taskManagement.setVisitors(1L);
        return success(iTaskManagementService.updateTaskManagementById(taskManagement));
    }




}
